//
//  LCInviteShareView.m
//  lechuang
//
//  Created by Mr_hi on 2024/8/28.
//  Copyright © 2024 Mooyi. All rights reserved.
//

#import "LCInviteShareView.h"
#import "XWXShareHelper.h"
#import "LCUserModel.h"

@interface LCInviteShareView ()

@property (strong, nonatomic)UIImage * shareImg;

@property (weak, nonatomic) IBOutlet UIView *alertView;

@property (weak, nonatomic) IBOutlet NSLayoutConstraint *bottomConstraint;

@end

@implementation LCInviteShareView

-(instancetype)initForNibWithShareImg:(UIImage *)shareImg {
    self = [[NSBundle mainBundle] loadNibNamed:NSStringFromClass(self.class) owner:nil options:nil][0];
    self.shareImg = shareImg;
    [self configUI];
    return self;
}

- (void)configUI {
    self.frame = CGRectMake(0, 0, ScreenW, ScreenH);
    [self whenTapped:^{
        [self dismiss];
    }];
    self.bottomConstraint.constant = -225;
    self.alpha = 0;
    [self layoutIfNeeded];
    [[UIApplication sharedApplication].keyWindow bringSubviewToFront:self];
}

-(void)show {
    [[UIApplication sharedApplication].keyWindow addSubview:self];
    [UIView animateWithDuration:0.25f animations:^{
        self.bottomConstraint.constant = 0;
        self.alpha = 1;
        [self layoutIfNeeded];
    }];
}

-(void)dismiss {
    [UIView animateWithDuration:0.25f animations:^{
        self.bottomConstraint.constant = -225;
        self.alpha = 0;
        [self layoutIfNeeded];
    } completion:^(BOOL finished) {
        [self.subviews makeObjectsPerformSelector:@selector(removeFromSuperview)];
        [self removeFromSuperview];
    }];
}

- (IBAction)wechatShareAction:(UIButton *)sender {
    [self dismiss];
    [XWXShareHelper shareWithController:self.parentVC andItems:@[self.shareImg] andCompletion:^(BOOL success) {
        if (success) {
            [self.parentVC showSuccessText:@"分享成功!"];
        }else{
            [self.parentVC showErrorText:@"分享失败!"];
        }
    }];
}

- (IBAction)linkShareAction:(UIButton *)sender {
    [self dismiss];
    LCUserModel * userModel = [LCUserModel mj_objectWithKeyValues:LCUserDefGet(UserInfo)];
    UIPasteboard *pab = [UIPasteboard generalPasteboard];
    [pab setString:userModel.share.url];
    if(pab == nil) {
        [self.parentVC showErrorText:@"复制失败"];
    }else{
        [self.parentVC showSuccessText:@"复制成功"];
    }
}

- (IBAction)cancelBtnAction:(UIButton *)sender {
    [self dismiss];
}

@end
